var router = require('koa-router')();

/** 渠道管理 start **/
router.post('/channel/list',async (ctx, next)=>{
    let {
        page=1,
        size=10,
    } = ctx.request.body;
    let count = await ctx.post('/admin/countIntroducer');
    let result = await ctx.post('/admin/listIntroducer',{page,size});
    if(count.cd==0){
        result.total = count.data;
    }
    ctx.body = result;
});
//get
router.post('/channel/get', async (ctx,next) => {
    let {id} = ctx.request.body;
    let result = {cd:0,data:{}};
    if(id){
        result = await ctx.post('/admin/getIntroducerById',{id});
    }
    let employee = await ctx.post('/admin/introducerRelatedEmployee');
    if(result.cd==0){
        delete result.data.create_id;
        delete result.data.created_at;
        delete result.data.update_id;
        delete result.data.updated_at;
    }
    ctx.body = {employee,result};
})
//保存
router.post('/channel/save', async (ctx,next) => {
    let {channel} = ctx.request.body;
    channel.type = parseInt(channel.type);
    channel.status = parseInt(channel.status);
    let result = null;
    console.log(channel,"------")
    if(channel.id){
        result = await ctx.post('/admin/updateIntroducer',{...channel})
    }else{
       result = await ctx.post('/admin/insertIntroducer',{...channel})
    }
    ctx.body = result;
})
//删除
router.post('/channel/delete' , async (ctx,next) => {
    let {id} = ctx.request.body;
    ctx.body = await ctx.post('/admin/deleteIntroducerById',{id});
})
//获得渠道归属人
router.post('/employee' , async (ctx,next) => {
    ctx.body = await ctx.post('/admin/introducerRelatedEmployee');
})
//修改状态
router.post('/channel/updateIntroducerStatus' , async (ctx,next) => {
    let {id,status} = ctx.request.body;
     status = status=='true'?1:0;
    ctx.body = await ctx.post('/admin/updateIntroducerStatus' , {id,status});
})
/** 渠道管理 end **/


/** 获客管理 start **/
router.post('/gainGuest/list' , async (ctx,next) => {
    let {
        page=1,
        size=10,
        nick_name='',
        introducer_name='',
        phone=''
    } = ctx.request.body;

    let count = await ctx.post('/admin/countUserProjectByIntroducer' , {nick_name,introducer_name,phone});
    let result = await ctx.post('/admin/listUserProjectByIntroducer',{page,size,nick_name,introducer_name,phone});
    if(count.cd==0 && result.cd==0){
        result.total = count.data;
    }
    ctx.body = result;
})
/** 获客管理 end **/


export default router;
